package com.sanyang.pda.ui.manualoutbound;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sanyang.pda.adapter.ManualOutWarehousingAdapter;
import com.sanyang.pda.bean.OutWarehousingBean;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.request.DeliveryConfirmRequest;
import com.sanyang.pda.contract.ManualOutWarehousingContract;
import com.sanyang.pda.dialog.ConfrimDialog;
import com.sanyang.pda.presenter.ManualOutWarehousingPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.utils.UserUtils;
import com.sanyang.pda.view.ScanButView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class ManualOutWarehousingFragment extends BaseMvpFragment<ManualOutWarehousingPresenter> implements ManualOutWarehousingContract.View, OnRefreshListener, OnLoadMoreListener {


    @BindView(R.id.rv_list)
    RecyclerView rvList;
    ManualOutWarehousingAdapter adapter;
    @BindView(R.id.bt_scan_location)
    ScanButView btScanLocation;
    @BindView(R.id.bt_add)
    Button btAdd;
    @BindView(R.id.bt_out_bound)
    Button btOutBound;
    @BindView(R.id.smart)
    SmartRefreshLayout smart;
    @BindView(R.id.tv_storage_location)
    TextView tvStorageLocation;
    private int num = 1;
    private String storagebinId;

    public ManualOutWarehousingFragment(String deliveryNo) {
        this.deliveryNo = deliveryNo;

    }

    private String deliveryNo;

    @Override
    public int getViewId() {
        return R.layout.fragment_manual_out;
    }

    @Override
    public void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ManualOutWarehousingAdapter(R.layout.item_out_warehousing, new ArrayList<>());
        rvList.setAdapter(adapter);
    }


    @Override
    public void initLinsenterner() {
        btScanLocation.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (scanBean.getType() == 2) {
                    storagebinId = scanBean.getCode();
                    tvStorageLocation.setText(storagebinId);
                    presenter.stackPalletInfo(num, deliveryNo, scanBean.getCode());
                }
            }
        });
    }

    @Override
    public void getData() {

    }

    @Override
    public ManualOutWarehousingPresenter getPresenter() {
        return new ManualOutWarehousingPresenter();
    }

    @OnClick({R.id.bt_add, R.id.bt_out_bound})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_add:
                if (TextUtils.isEmpty(storagebinId)) {
                    showMessage(getString(R.string.storage_location) + getString(R.string.not_be_empty));
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString(StringConfig.STORAGEBINID, storagebinId);
                bundle.putString(StringConfig.DELIVERYNO, deliveryNo);
                startIntent(AppActivityKey.ADDGOODSACTIVITY, bundle);
                break;
            case R.id.bt_out_bound:
                if (TextUtils.isEmpty(storagebinId)) {
                    showMessage(getString(R.string.storage_location) + getString(R.string.not_be_empty));
                    return;
                }
                if (adapter.getData().size() <= 0) {
                    showMessage(getString(R.string.goods) + getString(R.string.not_be_empty));
                    return;
                }
                ConfrimDialog confrimDialog = new ConfrimDialog(mContext, new ConfrimDialog.Onclick() {
                    @Override
                    public void onConfirm() {
                        DeliveryConfirmRequest deliveryConfirmRequest = new DeliveryConfirmRequest();
                        deliveryConfirmRequest.setDeliveryNo(deliveryNo);
                        deliveryConfirmRequest.setStoragebinId(storagebinId);
                        deliveryConfirmRequest.setOutedOperation(UserUtils.getInstance().getUserBean().getUserName());
                        presenter.deliveryConfirm(deliveryConfirmRequest);
                    }
                });
                confrimDialog.show(getString(R.string.confrimDialog));
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(storagebinId)) {
            presenter.stackPalletInfo(num, deliveryNo, storagebinId);
        }
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        num++;
        presenter.stackPalletInfo(num, deliveryNo, storagebinId);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        num = 1;
        presenter.stackPalletInfo(num, deliveryNo, storagebinId);
    }

    @Override
    public void setList(List<OutWarehousingBean> list) {
        smart.finishLoadMore();
        smart.finishRefresh();
        if (num == 1) {
            adapter.setNewInstance(list);
        } else {
            adapter.addData(list);
        }
    }

    @Override
    public void outSuccess() {
        adapter.setNewInstance(new ArrayList<>());
        storagebinId="";
        tvStorageLocation.setText("");
    }
}
